<?php
/**
 * Created by PhpStorm.
 * User: sojo
 * Date: 2017/12/28
 * Time: 14:17
 */

namespace App\Http\Admin\Administration\Models;

use App\Eloquent\Oa\WorkflowFile;
use Framework\BaseClass\Http\Admin\Model;

class WorkflowLog extends Model
{
    public function add($workflowId, $disposeCode, $operatorId, $assigneeId = 0, $opinion = '', $imageUrl = '', $enclosureUrl = '')
    {
        $note = WorkflowDict::getDescriptionByDisposeCode($disposeCode);

        //采购申请提示需特殊处理
        $workflowRow = \App\Eloquent\Oa\Workflow::where(['id'=>$workflowId])->first()->toArray();
        $relateType = $workflowRow['related_type'];
        if($relateType == 26 || $relateType == 27){//采购申请||退货申请
            //如果是创建，不提示
            if($disposeCode!=1000 && $disposeCode!=1100 && $disposeCode!=1200){
                //取上一个步骤的（因为到这一步时，已经是数据处理完了）
                $flowConfigProcessObj = \App\Eloquent\Oa\FlowConfigProcess::where(['pid'=>$workflowRow['oa_flow_config_process_id']])->first();
                if($flowConfigProcessObj){
                    //如果是采购申请&采购执行阶段 修改提示为 ‘执行’
                    if($relateType == 26 ){
                        $buttonValue = \App\Engine\WorkFlow::getPurchaseButtonByProcessId($flowConfigProcessObj->id);
                        if($buttonValue == 3){
                            $note = '执行';
                        }
                    }

                    $note = $flowConfigProcessObj->description.'·'.$note;
                }
            }elseif($disposeCode==1100){
                $note = \App\Eloquent\Oa\FlowConfigProcess::where(['id'=>$workflowRow['oa_flow_config_process_id']])->first()->description.'·'.$note;
            }elseif($disposeCode==1200){
                $note = \App\Eloquent\Oa\FlowConfigProcess::where(['id'=>$workflowRow['oa_flow_config_process_id']])->first()->description.'·'.$note;
            }
        }


        $workflow = new \App\Eloquent\Oa\WorkflowLog([
            'oa_workflow_id' => $workflowId,
            'dispose_code'   => $disposeCode,
            'operator_id'    => $operatorId,
            'assignee_id'    => $assigneeId,
            'opinion'        => $opinion,
            'note'           => $note
        ]);
        xAssert($workflow->save());
        $this->saveFile($workflowId, $workflow['id'], $imageUrl, $enclosureUrl);
        return;
    }

    public function saveFile($workflowId, $logId, $imageUrl = '', $enclosureUrl = '')
    {
        $time = time();
        $imageList = empty($imageUrl) ? [] : explode(',', $imageUrl);
        foreach ($imageList as &$file) {
            $tmp = explode('/', $file);
            $tmp = end($tmp);
            $oldName = explode('.', $tmp)[0];
            $file = [
                'oa_workflow_id'     => $workflowId,
                'oa_workflow_log_id' => $logId,
                'old_name'           => $oldName,
                'url'                => $file,
                'type'               => 1,
                'created_at'         => $time,
                'updated_at'         => $time,
            ];
        }
        $enclosureList = empty($enclosureUrl) ? [] : explode(',', $enclosureUrl);
        foreach ($enclosureList as &$file) {
            $tmp = explode('/', $file);
            $tmp = end($tmp);
            $oldName = explode('.', $tmp)[0];
            $file = [
                'oa_workflow_id'     => $workflowId,
                'oa_workflow_log_id' => $logId,
                'old_name'           => $oldName,
                'url'                => $file,
                'type'               => 2,
                'created_at'         => $time,
                'updated_at'         => $time,
            ];
        }
        $fileList = array_merge($imageList, $enclosureList);
        WorkflowFile::insert($fileList);
    }
}